drivers/serial: hold xmit.lock around echo in uart_readv()#19391
Open
94xhn wants to merge 1 commit into
Open
Conversation
uart_putxmitchar() manipulates dev->xmit.head/buffer directly with no internal locking - every other caller (uart_write()) takes dev->xmit.lock first. uart_readv()'s ECHO handling (both the backspace/delete erase sequence and the normal character echo) calls uart_putxmitchar() without taking that lock, so a concurrent uart_write() and a local echo can race on the same circular buffer state, corrupting it. Take dev->xmit.lock around each echo's uart_putxmitchar() calls, matching what uart_write() already does. uart_readv() holds dev->recv.lock for its own duration, but no other code path ever acquires recv.lock while holding xmit.lock, so nesting xmit.lock inside the existing recv.lock scope here doesn't introduce a new lock-ordering cycle. Fixes apache#14845 Signed-off-by: yi chen <94xhn1@gmail.com>
xiaoxiang781216
approved these changes
Jul 10, 2026
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14845.
uart_putxmitchar()manipulatesdev->xmit.head/dev->xmit.bufferdirectly with no locking of its own — every other caller takesdev->xmit.lockfirst (uart_write()locks once around its whole per-character write loop).uart_readv()'sECHOhandling callsuart_putxmitchar()in two places (the backspace/delete erase sequence, and normal character echo) without ever taking that lock, so a concurrentuart_write()and a local echo triggered byuart_readv()can race on the same circular transmit buffer state, corrupting it.Fix
Take
dev->xmit.lockaround each echo'suart_putxmitchar()call(s), matching whatuart_write()already does elsewhere in this same file.uart_readv()holdsdev->recv.lockfor the duration of its own read loop, so this introducesrecv.lock→xmit.locknesting. I checked every other use ofrecv.lock/xmit.lockin this file and no other code path ever acquiresrecv.lockwhile already holdingxmit.lock(nothing outsideuart_readv()touchesrecv.lockat all), so this doesn't create a new lock-ordering cycle/deadlock risk.Testing
I don't have real serial hardware or a working local NuttX
simbuild environment (WSL crashed on this machine and I wasn't able to get it running again), so I can't provide a build/run log or a genuine concurrent-echo-vs-write repro. What I did instead: read every call site ofuart_putxmitchar()and every use ofxmit.lock/recv.lockindrivers/serial/serial.cto confirm (a)uart_putxmitchar()really has no internal synchronization, (b)uart_write()'s existing lock/unlock pattern is the one to mirror, and (c) the lock-ordering argument above. Happy to add a stronger repro or runsim:nshif a maintainer can point me to (or confirm) a way to trigger it, e.g. via a two-thread test that concurrentlywrite()s and triggers local echo on the same tty.Generative AI
I used generative AI tools when creating this PR, but a human has checked the code and is responsible for the code and the description above.